<template>
  <div>
    <!-- 搜索 -->
    <Search ref="search" :fields="searchItem" :search="getList"></Search>
    <!-- 列表 -->
    <DataTable v-loading="listLoading" :list="list" :fields="fields" :permissions="$store.state.permissions" :add="toSave" :exports="toSave" :edit="toSave" :del="del" :batchDel="batchDel">
      <slot slot="buttonSlot" slot-scope="scope">
        <el-tooltip content="对象属性配置" class="operatingButton" v-if="$store.state.permissions.insert && $store.state.permissions.update">
          <el-link type="primary" icon="el-icon-s-tools" :underline="false" @click="openObjField(scope.row)">
          </el-link>
        </el-tooltip>
      </slot>
    </DataTable>
    <!-- 分页 -->
    <el-pagination style="float:right;margin-top: 10px;" background layout="total, sizes, prev, pager, next"
      :page-sizes="[5, 10, 15, 30]" :page-size="pageSize" :total="total" @current-change="changePage"
      @size-change="changeSize">
    </el-pagination>

    <!-- 表单 -->
    <Form ref="form" v-loading="formLoading" :title="title" :visible.sync="formVisible" :fields.sync="formFields" :formData="formData" :submitForm="save">
    </Form>

    <!-- 数据字段定义 -->
    <el-dialog :title="'对象属性配置 -- ' + thisObjName + '（' + thisObjCode + '）'" :visible.sync="objFieldVisible" width="80%"
      :before-close="cancelObjField" :close-on-click-modal="false">
      <BaseObjField :objCode="thisObjCode" :key="timer"></BaseObjField>
      <br /><br />
    </el-dialog>

  </div>
</template>

<script>
const BaseObjField = () => import("../BaseObjField");
export default {
  components: {
    BaseObjField
  },
  data() {
    return {
      title: "",
      formVisible: false,
      fields: [
        {
          label: "对象名称",
          value: "objName",
        },
        {
          label: "对象编码",
          value: "objCode",
        },
        {
          label: "创建时间",
          value: "createTime",
        },
      ],
      formFields: [
        {
          label: "对象名称",
          value: "objName",
          rules: [{ required: true, message: "对象名称不能为空!" }],
        },
        {
          label: "对象编码",
          value: "objCode",
          rules: [{ required: true, message: "对象编码不能为空!" }],
        },
      ],
      formData: {},
      formLoading: false,
      searchItem: [
        {
          label: "对象名称",
          value: "objName",
          type: "string",
        },
      ],
      list: [],
      listLoading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,

      objFieldVisible: false,
      thisObjCode: "",
      thisObjName: "",
      timer: ''
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    changeSize(pageSize) {
      this.pageSize = pageSize;
      this.$refs.search.search(this.$refs.search.param);
    },
    changePage(pageNum) {
      this.pageNum = pageNum;
      this.$refs.search.search(this.$refs.search.param);
    },
    openObjField(row) {
      this.thisObjCode = row.objCode;
      this.thisObjName = row.objName;
      this.timer = new Date().getTime();
      this.objFieldVisible = true;
    },
    cancelObjField() {
      this.thisObjCode = "";
      this.objFieldVisible = false;
    },
    del(uuid) {
      this.$api.baseObj.del({ uuid: uuid }).then(res => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    batchDel(uuids){
      this.$api.baseObj.batchDel({ uuids: uuids }).then(res => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    save() {
      this.$api.baseObj.save(this.formData).then(res => {
        if (res.code == 200) {
          this.$message.success("保存成功");
          this.formVisible = false;
          this.getList();
        } else {
          this.$message.error(res.msg);
          this.$refs.form.saveLoading = false;
        }
      })
    },
    toSave(uuid) {
      this.formData = {};
      if (!uuid) {
        this.title = "新增数据对象";
        this.formVisible = true;
      } else {
        this.title = "修改数据对象";
        this.$api.baseObj.getOne(uuid).then(res => {
          if (res.code == 200) {
            this.formData = res.data;
            this.formVisible = true;
          }else{
            this.$message.error(res.msg)
          }
        })
      }
      
    },
    getList(param) {
      this.listLoading = true;
      param = param ? param : {};
      param.pageNum = this.pageNum;
      param.pageSize = this.pageSize;

      this.list = [];
      this.$api.baseObj.list(param).then((res) => {
        if(res.code==200){
          this.list = res.data.list;
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.total = res.data.total;
        }else{
          this.$message.error(res.msg);
        }
        this.listLoading = false;
      });
    },
  },
};
</script>

<style>
</style>